package com.smmisia.article.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smmisia.article.config.WebSocketServer;
import com.smmisia.article.entity.GroupMember;
import com.smmisia.article.entity.GroupMessage;
import com.smmisia.article.entity.Message;
import com.smmisia.article.entity.convetor.GroupMessageConvertor;
import com.smmisia.article.entity.convetor.MessageConvertor;
import com.smmisia.article.exception.SocketException;
import com.smmisia.article.mapper.GroupMemberMapper;
import com.smmisia.article.mapper.GroupMessageMapper;
import com.smmisia.article.mapper.MessageMapper;
import com.smmisia.article.request.MessageRequest;
import com.smmisia.article.response.socket.GroupMessageResponse;
import com.smmisia.article.response.socket.MessageResponse;
import com.smmisia.article.service.MessageService;
import com.smmisia.common.Result.Result;
import com.smmisia.common.request.feign.request.FeignMessageGroupRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
    implements MessageService {

    @Autowired
    private GroupMessageMapper groupMessageMapper;

    @Autowired
    private GroupMemberMapper groupMemberMapper;

    @Autowired
    private WebSocketServer webSocketServer;

    @Override
    public Result send(MessageRequest request) {
        log.info("发送消息：{}", request);
        Message message = Message.create(request);
        boolean save = save(message);
        Assert.isTrue(save,()-> new SocketException("发送消息失败"));
        webSocketServer.sendOneMessage(String.valueOf(request.getUserId()),request.getContent(),(String) StpUtil.getLoginId());
        return Result.ok("发送成功");
    }

    @Override
    public Result<MessageResponse> getMessage(Integer sendId) {
        log.info("获取用户{}的消息", sendId);
        LambdaQueryWrapper<Message> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Message::getSendId, sendId)
                .eq(Message::getUserId, StpUtil.getLoginIdAsInt())
                .or()
                .eq(Message::getUserId, sendId)
                .eq(Message::getSendId, StpUtil.getLoginIdAsInt())
                .orderByAsc(Message::getCreateTime);
        MessageResponse messageResponse = new MessageResponse();
        messageResponse.setMessage(MessageConvertor.INSTANCE.mapToList(list(wrapper)));
        return Result.ok(messageResponse);
    }

    @Override
    public Result sendGroup(FeignMessageGroupRequest request) {
        GroupMessage groupMessage = GroupMessage.create(request);
        int result = groupMessageMapper.insert(groupMessage);
        Assert.isTrue(result > 0,()-> new SocketException("发送消息失败"));
        return Result.ok("发送成功");
    }

    @Override
    public Result<GroupMessageResponse> getGroupMessage(Integer groupId) {
        Integer loginId = StpUtil.getLoginIdAsInt();
        LambdaQueryWrapper<GroupMember> wrapper = new LambdaQueryWrapper<GroupMember>()
                .eq(GroupMember::getGroupId, groupId)
                .eq(GroupMember::getUserId, loginId);
        GroupMember groupMember = groupMemberMapper.selectOne(wrapper);
        Assert.notNull(groupMember,()-> new SocketException("用户不在该群聊中"));
        LambdaQueryWrapper<GroupMessage> groupMessageWrapper = Wrappers.lambdaQuery();
        groupMessageWrapper.eq(GroupMessage::getGroupId, groupId);
        List<GroupMessage> list = groupMessageMapper.selectList(groupMessageWrapper);
        GroupMessageResponse messageResponse = GroupMessageResponse
                .builder()
                .message(GroupMessageConvertor.INSTANCE.toList(list))
                .build();
        return Result.ok(messageResponse);

    }
}




